package com.mark.common.utils;

import com.mark.common.exception.DataException;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: 数据库执行结果处理工具类
 * @Author: Mark
 * @CreateDate: 2020/6/6 0:23
 * @Copyright : 豆浆油条个人非正式工作室
 */
public class DBExecutorResultUtil {

    private static final int SUCCESS_ROW = 1;
    private static final String DEFAULT_EXCEPTION_MSG = "操作失败！";

    private DBExecutorResultUtil() {
    }

    public static void effectRowIsNotOne(int effectCount) {
        effectRowIsNotOne(effectCount, DEFAULT_EXCEPTION_MSG);
    }

    /**
     * 如果数据库操作的返回的影响行数不是1则抛出<code>DataException<code/>异常
     *
     * @param effectCount  操作DB后的影响的行数
     * @param exceptionMsg 异常提示信息
     */
    public static void effectRowIsNotOne(int effectCount, String exceptionMsg) {
        if (StringUtils.isBlank(exceptionMsg)) {
            throw new IllegalArgumentException("输入的异常提示参数不能为空");
        }
        if (effectCount != SUCCESS_ROW) {
            throw new DataException(exceptionMsg);
        }
    }


    public static void isNull(Object dbObject) {
        isNull(dbObject, DEFAULT_EXCEPTION_MSG);
    }

    /**
     * 如果数据库操作的返回的对象是null则抛出<code>DataException<code/>异常
     *
     * @param dbObject     数据的查询结果对象
     * @param exceptionMsg 异常提示信息
     */
    public static void isNull(Object dbObject, String exceptionMsg) {
        if (null == dbObject) {
            throw new DataException(exceptionMsg);
        }
    }

    public static void isBlank(String dbResultStr) {
        isBlank(dbResultStr, DEFAULT_EXCEPTION_MSG);
    }

    /**
     * 如果数据库操作的返回的字符是空的则抛出<code>DataException<code/>异常
     *
     * @param dbResultStr 数据的查询结果
     * @param exceptionMsg 异常提示信息
     */
    public static void isBlank(String dbResultStr, String exceptionMsg) {
        if(StringUtils.isBlank(dbResultStr)){
            throw new DataException(exceptionMsg);
        }
    }
}
